/* Written 8/12/93 by Max Hailperin <max@nic.gac.edu>, Math/CS department,
   Gustavus Adolphus College.  Public domain, no warranty.  Please share
   any improvements with me.  */

/* Generated by Interface Builder */

#import "StatusMonitor.h"
#import "GACUPSSpeaker.h"
#import <appkit/appkit.h>
#import <dpsclient/dpsclient.h>
#import <time.h>

#define PERIOD 5.0

static void handleTE(DPSTimedEntry te, double now, void *monitor){
  [(id)monitor update:(id)monitor];
}

static char *timeString(time_t time){
  static char buf[100];
  strftime(buf, sizeof(buf), "%X", localtime((time_t*)&time));
  return buf;
}

static char *tenthsToStr(int tenths){
  static char buf[100];
  sprintf(buf, "%d.%d", tenths/10, tenths%10);
  return buf;
}

  
@implementation StatusMonitor

- appDidInit:sender
{
    NXDefaultsVector defaults = {{"Hostname", "localhost"}, {NULL, NULL}};
    NXRegisterDefaults([NXApp appName], defaults);
    [hostnameField
      setStringValue:NXGetDefaultValue([NXApp appName], "Hostname")];
    [panel makeKeyAndOrderFront:self];
    [hostnameField selectText:self];
    speaker = [[GACUPSSpeaker alloc] init];
    return [self newHost:hostnameField];
}

- windowWillClose:sender
{
    [NXApp terminate:self];
    return self;
}

- update:sender
{
  int systemMode, inverterStatus, alarmStatus, ACInputVolts, ACOutputVolts,
  ACOutputDeciAmperes, ACLoadVA, batteryDeciVolts, deciHertz, minutes,
  ambientTemp, ROMVersion, time;

  if(![speaker systemMode: &systemMode
        inverterStatus: &inverterStatus
        alarmStatus: &alarmStatus
	ACInputVolts: &ACInputVolts
	ACOutputVolts: &ACOutputVolts
	ACOutputDeciAmperes: &ACOutputDeciAmperes
	ACLoadVA: &ACLoadVA
	batteryDeciVolts: &batteryDeciVolts
	deciHertz: &deciHertz
	minutes: &minutes
	ambientTemp: &ambientTemp
	ROMVersion: &ROMVersion
	time: &time]){
    [systemModeCell setStringValue:(systemMode?"Normal":"Shutdown")];
    [inverterStatusCell setStringValue:(inverterStatus?"On":"Off")];
#ifdef FAKEALARMS
    alarmStatus = FAKEALARMS;   // to help me debug
#endif
    {int i;
     for(i = 0; i < [alarms cellCount]; i++)
       [[alarms findCellWithTag:i] setTextGray:
        ((alarmStatus&(1<<i))?NX_BLACK:NX_DKGRAY)];
    }
    [ACInputVoltsCell setIntValue:ACInputVolts];
    [ACOutputVoltsCell setIntValue:ACOutputVolts];
    [ACOutputAmperesCell setStringValue:tenthsToStr(ACOutputDeciAmperes)];
    [ACLoadVACell setIntValue:ACLoadVA];
    [batteryVoltageCell setStringValue:tenthsToStr(batteryDeciVolts)];
    [frequencyCell setStringValue:tenthsToStr(deciHertz)];
    [minutesCell setIntValue:minutes];
    [ambientTempCell setIntValue:ambientTemp];
    [ROMRevisionCell setIntValue:ROMVersion];
    [timeCell setStringValue:timeString(time)];
  }
  return self;
}

- newHost:sender
{
    static DPSTimedEntry te;
    static char portName[1024];
    port_t daemonPort;
    if(te){
        DPSRemoveTimedEntry(te);
        te = 0;
      }
    NXWriteDefault([NXApp appName], "Hostname", [sender stringValue]);
    if(!*portName) sprintf(portName, "%s.daemon", [NXApp appName]);
    daemonPort = NXPortFromName(portName, [sender stringValue]);
    if(daemonPort == PORT_NULL){
      NXRunAlertPanel([NXApp appName], "No GACUPS daemon found on this host.",
        "OK", NULL, NULL);
    } else {
      [speaker setSendPort:daemonPort]; 
      [self update:self];
      te = DPSAddTimedEntry(PERIOD, handleTE, (void*)self, NX_BASETHRESHOLD);
    }
    return self;
}


@end
